package cn.edu.sdjzu.xg.bysj.controller;

import cn.edu.sdjzu.xg.bysj.domain.Condition;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.Pagination;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.service.DepartmentService;
import cn.edu.sdjzu.xg.bysj.util.ControllerHelper;
import cn.edu.sdjzu.xg.bysj.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;


@WebServlet("/departmentRead.ctl")
public class DepartmentReadCtrl extends HttpServlet {
    // find（int id）
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // 设置响应字符编码为UTF8，以便正确响应汉字
        response.setContentType("application/json;charset=UTF-8");
        // 创建JSON对象message，以便往前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        // 将request body转为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        // 将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        // 如果请求body为空
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息！");
            response.getWriter().println(message_jsonObj);
            return;
        }

        try{
            int id = req_jsonObj.getInteger("id");
            // 分页获得符合条件的教师对象
            Department department = DepartmentService.getInstance().find(id);
            message_jsonObj.put("records",department);
            // 响应department_json到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e) {
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            // 每个都要有printStackTrace
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
    // 条件查询
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // 设置响应字符编码为UTF8，以便正确响应汉字
        response.setContentType("application/json;charset=UTF-8");
        // 创建JSON对象message，以便往前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        // 将request body转为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        System.out.println(req_jsonStr);
        // 将request body解析为JSONObject对象，可能含有分页信息、查询条件
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        // 如果请求body为空
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息！");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try{
            // 从请求JSONObject对象中读取键“conditionList”对应的值（描述查询条件的JSONArray对象）
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            // 把condition给teacherService，追加到查询语句后方
            Pagination pagination = null;
            Collection<Department> departmentList = DepartmentService.getInstance().findAll(conditionList, pagination);
            String department_json = JSON.toJSONString(departmentList,SerializerFeature.DisableCircularReferenceDetect);
            // 响应department_json到前端
            response.getWriter().println(department_json);
        }catch (BysjException e) {
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            // 每个都要有printStackTrace
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }

    // 分页查询和条件查询
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // 设置响应字符编码为UTF8，以便正确响应汉字
        response.setContentType("application/json;charset=UTF-8");
        // 创建JSON对象message，以便往前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        // 将request body转为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        // 将request body解析为JSONObject对象，可能含有分页信息、查询条件
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        // 如果请求body为空
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息！");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try{
            // 读取分页信息，形成分页对象
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            // 从请求JSONObject对象中读取键“conditionList”对应的值（描述查询条件的JSONArray对象）
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            // 分页获得符合条件的教师对象
            Collection<Department> departments = DepartmentService.getInstance().findAll(conditionList,pagination);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("records",departments);
            // 响应teacher_json到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e) {
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            // 每个都要有printStackTrace
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
}
